package com.ruoyi.web.controller.homestay;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.system.bean.merchant.dto.AddMerchant;
import com.ruoyi.system.bean.merchant.query.MerchantQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Merchant;
import com.ruoyi.system.service.IMerchantService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商家Controller
 * 
 * @author ruoyi
 * @date 2024-03-04
 */
@Api(tags = "商家管理")
@RestController
@RequestMapping("/homestay/merchant")
public class MerchantController extends BaseController
{
    @Autowired
    private IMerchantService merchantService;

    /**
     * 查询商家列表
     */
    @PreAuthorize("@ss.hasPermi('homestay:merchant:list')")
    @ApiOperation("查询商家列表")
    @PostMapping ("/list")
//    @LogPrint
    public TableDataInfo list(@RequestBody MerchantQuery merchantQuery)
    {
        startPage();
        List<Merchant> list = merchantService.selectMerList(merchantQuery);
        return getDataTable(list);
    }

    /**
     * 导出商家列表
     */
    @PreAuthorize("@ss.hasPermi('homestay:merchant:export')")
    @ApiOperation("导出商家列表")
    @Log(title = "商家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Merchant merchant)
    {
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        ExcelUtil<Merchant> util = new ExcelUtil<Merchant>(Merchant.class);
        util.exportExcel(response, list, "商家数据");
    }

    /**
     * 获取商家详细信息
     */
    @PreAuthorize("@ss.hasPermi('homestay:merchant:query')")
    @ApiOperation("获取商家详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(merchantService.selectMerchantById(id));
    }

    /**
     * 新增商家
     */
    @PreAuthorize("@ss.hasPermi('homestay:merchant:add')")
    @ApiOperation("新增商家")
    @PostMapping(value = "/addMerchant", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addMerchant", value = "商家新增对象", dataType = "AddMerchant", paramType = "body")
    })
    public AjaxResult addMerchant(@RequestBody MerchantQuery merchantQuery) {
        AddMerchant addMerchant = new AddMerchant();
        return AjaxResult.success(merchantService.insertMerchant(addMerchant));
    }

    /**
     * 修改商家
     */
    @PreAuthorize("@ss.hasPermi('homestay:merchant:edit')")
    @ApiOperation("修改商家")
    //@Log(title = "商家", businessType = BusinessType.UPDATE)
    @PostMapping("/editMerchant")
    public AjaxResult edit(@RequestBody AddMerchant addMerchant)
    {
        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(addMerchant,merchant);
        return toAjax(merchantService.updateMerchant(merchant));
    }

    /**
     * 删除商家
     */
    @PreAuthorize("@ss.hasPermi('homestay:merchant:remove')")
    @ApiOperation("删除商家")
    @Log(title = "商家", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(merchantService.deleteMerchantByIds(ids));
    }
}
